﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class FriendlyLinkBll : IFriendlyLinkBll
    {
        private IFriendlyLinkDal _friend;
        private IMapper _mapper;
        public FriendlyLinkBll(IFriendlyLinkDal friend, IMapper mapper)
        {
            _friend = friend;
            _mapper = mapper;
        }

        public async Task<int> AddFriendlyLinkAsync(string title, string link, string photo)
        {
            return await _friend.AddAsync(new Friendlylink 
            {
                Title = title,
                Link = link,
                Photo = photo
            });
        }

        public async Task<int> DeleteFriendlyLinkAsync(int id)
        {
            var data = await _friend.QueryAsync(id);
            return data == null ? -1 : await _friend.DeleteAsync(data);
        }

        public async Task<int> EditFriendlyLinkAsync(int id, string title, string link, string photo)
        {
            var data = await _friend.QueryAsync(id);
            if (data == null)
                return -1;
            data.Title = title;
            data.Link = link;
            data.Photo = photo;
            return await _friend.EditAsync(data);
        }

        public async Task<List<FriendlyLinkDto>> GetFriendlyLinkAllAsync()
        {
            var data = await _friend.QueryAsync();
            return _mapper.Map<List<Friendlylink>,List<FriendlyLinkDto>>(data);
        }

        public async Task<FriendlyLinkDto> QueryFriendlyLinkByIdAsync(int id)
        {
            var data = await _friend.QueryAsync(id);
            return _mapper.Map<Friendlylink, FriendlyLinkDto>(data);
        }
    }
}
